/*
 * Copyright (c) 2009 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.wtk.content;

import wtkx.Component;
import wtkx.FlowPane;
import wtkx.ImageView;
import wtkx.Label;
import wtkx.TreeView;
import wtkx.io.Folder;
import wtkx.wtk.HorizontalAlignment;
import wtkx.wtk.VerticalAlignment;
import wtkx.wtk.media.Image;
import wtkx.wtk.media.Picture;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * Tree view decorator for displaying file system contents.
 *
 * @author gbrown
 */
public class TreeViewFileDecorator
    extends FlowPane
    implements TreeView.NodeDecorator
{

    public static final int ICON_WIDTH = 16;
    public static final int ICON_HEIGHT = 16;

    private static final Image defaultFolderImage =
        Image.load(TreeViewFileDecorator.class.getResource("folder.png"));
    private static final Image defaultFileImage =
        Image.load(TreeViewFileDecorator.class.getResource("page_white.png"));


    private ImageView imageView = new ImageView();

    private Label label = new Label();


    public TreeViewFileDecorator() {
        super();
        Component.StyleDictionary styles = this.getStyles();

        styles.put("horizontalAlignment", HorizontalAlignment.LEFT);
        styles.put("verticalAlignment", VerticalAlignment.CENTER);

        this.add(this.imageView);
        this.add(this.label);

        this.imageView.setPreferredSize(ICON_WIDTH, ICON_HEIGHT);
    }


    public void setSize(int width, int height) {
        super.setSize(width, height);

        // Since this component doesn't have a parent, it won't be validated
        // via layout; ensure that it is valid here
        this.validate();
    }

    @Override
    public int getPreferredHeight(int width) {
        int preferredHeight = super.getPreferredHeight(width);
        return preferredHeight;
    }

    public void render(Object node, TreeView treeView, boolean expanded,
                       boolean selected, TreeView.NodeCheckState checkState,
                       boolean highlighted, boolean disabled)
    {
        File file = (File)node;

        Image icon = null;


        if (file instanceof Folder) {
            icon = defaultFolderImage;
        } else {
            icon = defaultFileImage;
        }

        imageView.setImage(icon);
        imageView.getStyles().put("opacity",
            (treeView.isEnabled() && !disabled) ? 1.0f : 0.5f);

        // Update the label
        label.setText(file.getName());

        Object labelFont = treeView.getStyles().get("font");
        if (labelFont instanceof Font) {
            label.getStyles().put("font", labelFont);
        }

        Object color = null;
        if (treeView.isEnabled() && !disabled) {
            if (selected) {
                if (treeView.isFocused()) {
                    color = treeView.getStyles().get("selectionColor");
                } else {
                    color = treeView.getStyles().get("inactiveSelectionColor");
                }
            } else {
                color = treeView.getStyles().get("color");
            }
        } else {
            color = treeView.getStyles().get("disabledColor");
        }

        if (color instanceof Color) {
            label.getStyles().put("color", color);
        }
    }

}
